package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.RegisterCodeResponse;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.KaptchaCodeRequest;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.form.UserVerifyRequest;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import com.cskaoyan.user.service.IUserRegisterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.cskaoyan.bean.Userinfo;

/**
 * @author: 杨庆
 * @since: 2022.9.19 22:09
 * @description:
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    IKaptchaService iKaptchaService;
    @Autowired
    IUserRegisterService iUserRegisterService;
    @Autowired
    ILoginService iLoginService;

    /**
     * @description: 用户的注册
     * []
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     * @author: 杨庆
     * @time: 2022.9.19 22:10
     */
    @RequestMapping("register")
    public ResponseData register(@RequestBody @Valid  UserRegisterRequest userRegisterRequest, HttpServletRequest request) {
        //验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String kaptcha_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        System.out.println(kaptcha_uuid);
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        kaptchaCodeRequest.setCode(userRegisterRequest.getCaptcha());
        iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        //TODO 注册 向用户表插入记录
        RegisterCodeResponse register = iUserRegisterService.register(userRegisterRequest);
        if (register.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<ResponseData>().setData(null);
        }
        //TOdo 返回的数据信息
        return new ResponseUtil().setErrorMsg(register.getMessage());
    }

    /**
    * @description: 用户的登录
    [userLoginRequest, request, response]
    * @return: com.cskaoyan.mall.commons.result.ResponseData
    * @author: 杨庆
    * @time: 2022.9.20 22:40
     */
    @PostMapping("login")
    public ResponseData login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request,
                              HttpServletResponse response) {
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String kaptcha_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        kaptchaCodeRequest.setCode(userLoginRequest.getCaptcha());
        iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        //TODO  验证码验证成功以后吗，查询数据库
        UserLoginResponse login = iLoginService.login(userLoginRequest);
        if (login == null) {
            return new  ResponseUtil().setErrorMsg("登陆失败");
        }
        // {"uid":1, "username":"zs"}
        //TODo 定义一个userinfo json对象 uid username
        Userinfo userinfo = new Userinfo();
        userinfo.setUid(login.getId());
        userinfo.setUsername(login.getUsername());
        //TOdo 转换为json的zi字符串
        String userinfo1 = JSON.toJSONString(userinfo);
        String token = JwtTokenUtils.builder().msg(userinfo1).build().creatJwtToken();
        login.setToken(token);
        //TODO 设置 access_token
        Cookie cookie = CookieUtil.genCookie("access_token", token, "/", 60*60);
        response.addCookie(cookie);
        return new ResponseUtil<UserLoginResponse>().setData(login);
    }

    /**
    * @description: 用户的验证
    [request]
    * @return: com.cskaoyan.mall.commons.result.ResponseData
    * @author: 杨庆
    * @time: 2022.9.20 23:14
     */
    @GetMapping("login")
    public ResponseData autoLogin(HttpServletRequest request) {
        String user_info = request.getHeader("user_info");
        System.out.println("user_info = " + user_info);
        if (StringUtils.isEmpty(user_info)) {
            //没有登登录
            return new  ResponseUtil().setErrorMsg("没有登录");
        }
        Userinfo userinfo = JSON.parseObject(user_info, Userinfo.class);
        return new ResponseUtil<Userinfo>().setData(userinfo);
    }

    /**
    * @description:退出登录
    [request, response]
    * @return: com.cskaoyan.mall.commons.result.ResponseData
    * @author: 杨庆
    * @time: 2022.9.20 23:15
     */
    @GetMapping("loginOut")
    public ResponseData loginOut(HttpServletRequest request,HttpServletResponse response) {
        Cookie cookie = new Cookie("access_token", null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
        return new ResponseUtil<>().setData(null);
    }

    /**
    * @description: 用户的激活
    [userVerifyRequest]
    * @return: com.cskaoyan.mall.commons.result.ResponseData
    * @author: 杨庆
    * @time: 2022.9.20 23:15
     */
    @GetMapping("verify")
    public ResponseData verfiy(UserVerifyRequest userVerifyRequest) {
        RegisterCodeResponse verify = iUserRegisterService.verify(userVerifyRequest);
        if (verify.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null,"激活成功");
        }
        return new ResponseUtil<>().setErrorMsg(verify.getMessage());
    }
}
